package com.zywl.web.controller.sys;

import com.zywl.core.beans.Setmeal;
import com.zywl.core.beans.User;
import com.zywl.service.SetmealService;
import com.zywl.service.MemberService;
import com.zywl.utils.models.AjaxResult;
import com.zywl.utils.models.PageData;
import com.zywl.utils.models.PageResult;
import com.zywl.utils.utils.Constants;
import com.zywl.utils.utils.MapUtil;
import com.zywl.utils.utils.enums.LogTypeEnum;
import com.zywl.web.controller.base.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController extends BaseController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private MemberService memberService;

    @GetMapping("/setmealListPage")
    public ModelAndView setmealListPage() {
        ModelAndView mav = new ModelAndView("setmeal/setmealListPage");
        return mav;
    }


    @PostMapping("/getSetmealList")
    public PageResult getSetmealList(HttpServletRequest request, PageData pageData) {

        Map<String, Object> condition = MapUtil.changeMap(request.getParameterMap());
        PageResult pageResult = null;
        try {
            condition.put("semStt","0");
            pageResult = putToResult(setmealService.getPageInfo(condition, pageData));
        } catch (Exception e) {
            logger.error("获取套餐列表异常", e);
        }
        return pageResult;
    }

    @GetMapping("/setmealSubmitPage")
    public ModelAndView setmealSubmitPage(String id, HttpSession session) {
        ModelAndView mav = new ModelAndView("setmeal/setmealSubmitPage");
        try {
            if (StringUtils.isNotEmpty(id)) {
                Setmeal setmeal = setmealService.getById(Long.parseLong(id));
                mav.addObject("setmeal", setmeal);
            }
        } catch (Exception e) {
            logger.error("获取数据异常", e);
        }
        return mav;
    }

    @PostMapping("/setmealSubmit")
    public AjaxResult setmealSubmit(HttpServletRequest request, Setmeal form) {
        AjaxResult ar = new AjaxResult();
        if (form != null) {
            Setmeal setmeal = null;
            String old = null;
            Date date = new Date();
            try {
                if (form.getSemId() != null) {
                    setmeal = setmealService.getById(form.getSemId());
                    old = setmeal.getSemName();
                    form.setSemAssociationId(setmeal.getSemAssociationId());
                    form.setSemStt(setmeal.getSemStt());
                    form.setSemCreateTime(setmeal.getSemCreateTime());
                } else {
                    setmeal = new Setmeal();
                    form.setSemStt("0");
                    form.setSemCreateTime(date);

                    User user = (User) request.getSession().getAttribute(Constants.USER_SESSION_KEY);
                    if (user.getUsrAssociationId() != null){
                        form.setSemAssociationId(user.getUsrAssociationId());
                    }else{
                        ar.error("平台管理员无法帮忙创建收费套餐");
                        return ar;
                    }
                }

                form.setSemModifyTime(date);
                BeanUtils.copyProperties(form, setmeal);

                if (form.getSemId() == null) {
                    setmealService.save(setmeal);
                } else {
                    setmealService.update(setmeal);
                }
            } catch (Exception e) {
                logger.error("提交套餐信息异常", e);
                ar.error("提交套餐信息异常", e);
                return ar;
            }

            if (form.getSemId() == null) {
                saveLog(request, LogTypeEnum.SETMEAL.getKey(), "套餐新增-" + form.getSemName());
                ar.success("新增成功");
            } else if (old != null && !old.equals(form.getSemName())) {
                saveLog(request, LogTypeEnum.SETMEAL.getKey(), "套餐修改-" + old + "→" + form.getSemName());
                ar.success("修改成功");
            } else {
                saveLog(request, LogTypeEnum.SETMEAL.getKey(), "套餐修改-" + form.getSemName());
                ar.success("修改成功");
            }
        } else {
            ar.error("提交失败，参数为空");
        }
        return ar;
    }

    @PostMapping("/setmealDelete")
    public AjaxResult setmealDelete(HttpServletRequest request, Long id) {
        AjaxResult ar = new AjaxResult();
        try {
            Setmeal setmeal = setmealService.getById(id);
            String setmealNickName = setmeal.getSemName();
            setmeal.setSemStt("1");
            setmealService.update(setmeal);//逻辑删除用户
            saveLog(request, LogTypeEnum.SETMEAL.getKey(), "套餐删除-" + setmealNickName);
            ar.success("删除成功");
        } catch (Exception e) {
            ar.error("删除套餐异常", e);
            logger.error("删除套餐异常", e);
        }
        return ar;
    }
}
